<?Lassoscript
// Last modified 6/28/11 by Eric Landmann

/*
Tagdocs;
	{Tagname=			LI_Log }
	{Description=		Logs information to a specified logfile }
	{Author=			Kyle Jessup }
	{AuthorEmail=		kyle@lassosoft.com }
	{ModifiedBy=		Eric Landmann }
	{ModifiedByEmail=	elandmann@landmanninteractive.com }
	{Date=				6/28/11 }
	{Usage=				LI_Log($svLogFile, 'Log this line.'); }
	{ExpectedResults=	A line logged to the logfile }
	{Dependencies=		None }
	{DevelNotes=		 }
	{ChangeNotes=		6/28/11
						First implementation 
						Original code came from Lasso9 Beta list, subject "Log_Destination_File", 1/24/11 }
/Tagdocs;
*/

Define LI_Log_Thread => thread {
	data private logMap = map
	
	public addData(filePath, data) => {
		.logMap !>> #filePath?
			.logMap->insert(#filePath = #data)
			| .logMap->get(#filePath)->append(#data)
	}
	public active_tick() => {
		with pr in .logMap->eachPair
		do file->openAppend(#pr->first, true) & writeString(#pr->second) & close
		.logMap->clear
		
		return 2 // sleep for 2 seconds
	}
}

Define LI_Log(path, data) => {
	LI_Log_Thread->addData(file_forceRoot+#path, '['+date+'] '+string(#data)+'\n')
}
// TESTING
/*
Loop(50);
	LI_Log('/logs/error.log', Loop_Count + ' This is the data to log')
/Loop;
*/
?>